//
//  QDSupportViewControler.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/2/25.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

/// 帮助与客服
final class QDSupportViewControler: QDOriginPageViewController {
    
    //override
    override var titles: [String] {
        get {
            return ["APP常见问题","乘车常见问题"]
        }
    }
    
    override var pageControllers: [UIViewController] {
        get {
            return [QDOriginTitleTableViewController(),QDOriginTitleTableViewController(type: .questionForMetro)]
        }
    }
    
    /// 浮动窗口
    private let floatImageView = UIImageView()
    /// 显示购买记录
    private let floatLabel = UILabel()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "帮助与客服"

        // 浮动
        floatImageView.image = "float_background".namedImage
        floatImageView.ritl_size = CGSize(width: 70, height: 70)
        floatImageView.isUserInteractionEnabled = true
        
        floatLabel.text = "联系客服"
        floatLabel.textColor = .white
        floatLabel.font = Font.regular.font(size: 14)
        floatLabel.numberOfLines = 2
        
        floatImageView.addUIControlHandler { [unowned self](_) in
            print("联系客服")
            self.checkLogin {
                self.ritl_topNavigationPush(QDQuestionFeedViewController(), animated: true) //追加登陆
            }
        }
        
        //追加拖动手势
        let panRecongnizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(recongnizer:)))
        floatImageView.addGestureRecognizer(panRecongnizer)
        
        view.addSubview(floatImageView)
        floatImageView.addSubview(floatLabel)
        
        floatImageView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().inset(9)
            make.height.width.equalTo(70)
            make.bottom.equalToSuperview().inset(BarDistance.TabBar.height)
        }
        
        floatLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(12)
            make.height.equalTo(40)
            make.width.equalTo(28)
            make.centerX.equalToSuperview()
        }
    }
    
    
    /// 处理拖动手势
    @objc func handlePan(recongnizer: UIPanGestureRecognizer) {
        
        /// 执行动画
        func removeFloatImageViewAnimated(){
            
            //获得中点
            let centerX = floatImageView.center.x
            let centerY = floatImageView.center.y
            
            var toPointX = 0
            var toPointY = centerY
            let duration = CGFloat(0.8)
            var distance = CGFloat(0.0) //距离
            let maxDistance = ritl_width / 2.0 - 70 / 2
            
            if centerX < ritl_width / 2.0 {//位于左半屏
                toPointX = Int(0.5 * 70) + 9
            }else {
                toPointX = Int(ritl_width - 70 * 0.5) - 9
            }
            
            let minCenterY = 70 * 0.5 + 10 + BarDistance.NavigationBar.height//上边距10
            let maxCenterY = ritl_height - 70 * 0.5 - BarDistance.TabBar.height
            
            if centerY < CGFloat(minCenterY) {//太过往上
                toPointY = CGFloat(minCenterY)
            }else if centerY > CGFloat(maxCenterY) {
                toPointY = maxCenterY
            }
            
            distance = abs(CGFloat(toPointX) - centerX)
            let timeScale = CGFloat(distance) / maxDistance
            
            // 执行动画
            UIView.animate(withDuration: TimeInterval(duration * timeScale), delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.3, options: .curveLinear, animations: {
                
                self.floatImageView.ritl_centerX = CGFloat(toPointX)
                self.floatImageView.ritl_centerY = toPointY
                
            }, completion: nil)
        }
        
        let point = recongnizer.location(in: recongnizer.view)//获得point
        let converPoint = recongnizer.view?.convert(point, to: view)//进行坐标转换
        floatImageView.center = converPoint ?? .zero
        
        switch recongnizer.state {
        case .ended: fallthrough
        case .cancelled: fallthrough
        case .failed: removeFloatImageViewAnimated()
            
        default: break
        }
    }
    
}
